const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

import {
	formatDate
} from '@/config/common.js';

import {
	errorToShow
} from '@/config/common.js';

function checkPermission(value) {
	let permission = uni.getStorageSync('userPermission');
	if (permission) {
		const permissionList = permission.split(",");
		if (value && value instanceof Array && value.length > 0) {
			const roles = permissionList;
			const permissionRoles = value
			const hasPermission = roles.some(role => {
				return permissionRoles.includes(role)
			})
			return hasPermission
		} else {
			console.error(`need roles! Like v-permission="['admin','editor']"`)
			return false
		}
	}
	return false;
}


function base64Encode(input) {
	let output = "";
	let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	let i = 0;
	input = baseUtf8Encode(input);
	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
		output = output + _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
			_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
	}
	return output;
}

function base64Decode(input) {
	let output = "";
	let chr1, chr2, chr3;
	let enc1, enc2, enc3, enc4;
	let i = 0;
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	while (i < input.length) {
		enc1 = _keyStr.indexOf(input.charAt(i++));
		enc2 = _keyStr.indexOf(input.charAt(i++));
		enc3 = _keyStr.indexOf(input.charAt(i++));
		enc4 = _keyStr.indexOf(input.charAt(i++));
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
		output = output + String.fromCharCode(chr1);
		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
	}
	return output;
}


function baseUtf8Encode(value) {
	value = value.replace(/\r\n/g, "\n");
	let utftext = "";
	for (let n = 0; n < value.length; n++) {
		let c = value.charCodeAt(n);
		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if ((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return utftext;
}

function baseUtf8Decode(utftext) {
	let string = "";
	let i = 0;
	let c = c1 = c2 = 0;
	while (i < utftext.length) {
		c = utftext.charCodeAt(i);
		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		} else if ((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i + 1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		} else {
			c2 = utftext.charCodeAt(i + 1);
			c3 = utftext.charCodeAt(i + 2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}
	}
	return string;
}

function checkWetchatEnableYueWan(value) {
	let wetchatEnableYueWan = uni.getStorageSync('wetchatEnableYueWan');
	return wetchatEnableYueWan == "1";
}


//计算时间差-分钟
function timeDifference(time1, time2) {
	//判断开始时间是否大于结束日期
	if (time1 > time2) {
		errorToShow("开始时间不能大于结束时间！");
		return false;
	}
	//截取字符串
	var begin1 = time1.substr(0, 10).split("-");
	var end1 = time2.substr(0, 10).split("-");
	//将拆分的数组重新组合，并实例成化新的日期对象
	var date1 = new Date(begin1[0] + - +begin1[1] + - +begin1[2]);
	var date2 = new Date(end1[0] + - +end1[1] + - +end1[2]);
	//得到两个日期之间的差值m，以分钟为单位
	var m = parseInt(Math.abs(date2 - date1) / 1000 / 60);
	var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
	var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));
	//两个分钟数相减得到时间部分的差值，以分钟为单位
	var n = min2 - min1;
	var sec1 = parseInt(time1.substr(17, 2)) / 60;
	var sec2 = parseInt(time2.substr(17, 2)) / 60;
	var s = sec2 - sec1;

	//将日期和时间两个部分计算出来的差值相加，即得到两个时间相减后的分钟数
	var minutes = m + n + s;
	return minutes.toFixed(2);
}

function checkValIsNumber(val) {
	if (!val) {
		return false;
	}
	if (!(/(^[1-9]\d*$)/.test(val))) {
		return false;
	} else {
		return true;
	}
}


function getCurrentLocation() {
	return new Promise(function(resolve, reject) {
		uni.authorize({
			scope: 'scope.userLocation',
			success() {
			
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log(res,'定位')
						let result = {
							longitude: res.longitude,
							latitude: res.latitude,
						}
						resolve(res)
					},
					complete(res) {
						console.log(res,'定位33')
					}
				});
			},
			fail() {
				// errorToShow("获取位置信息失败");
				reject('error')
			}
		})
	});
}


function createPhoneCall(phoneNumber) {
	if (!phoneNumber) {
		errorToShow("电话号码错误");
		return;
	}
	uni.makePhoneCall({
		phoneNumber: phoneNumber,
		fail() {
			// errorToShow("拨打电话失败");
		}
	})
}


function subStringLength(value, endLength, beginLength = 0) {
	if (!value) {
		return "";
	}
	if (value.length < endLength) {
		return value;
	}
	return value.substr(beginLength, endLength) + "...";
}


export {
	checkPermission,
	checkWetchatEnableYueWan,
	base64Encode,
	base64Decode,
	timeDifference,
	checkValIsNumber,
	getCurrentLocation,
	subStringLength,
	createPhoneCall
}
